echo '#!/bin/bash
if [[ $(find /etc/first-run.d/ -maxdepth 1 -name "*.sh") ]]; then
    for i in $(ls /etc/first-run.d/*.sh | sort -n ) ; do
        if [ -f "$i" ]; then
            chmod +x $i
            $i
            if [ "$?" == "0" ]; then
                if [ -f "$i" ]; then
                    rm $i
                fi
            fi
        fi
    done
fi

if [ -x /usr/bin/run-startx.sh ]; then
    /usr/bin/run-startx.sh &
fi
' > ${SYSDIR}/sysroot/etc/rc.local
chmod +x ${SYSDIR}/sysroot/etc/rc.local

mkdir -pv ${SYSDIR}/sysroot/etc/first-run.d/

cat > ${SYSDIR}/sysroot/etc/first-run.d/000-os-first.sh << EOF
#!/bin/bash -e
pwconv
grpconv
ssh-keygen -A
update-mime-database /usr/share/mime
glib-compile-schemas /usr/share/glib-2.0/schemas
systemctl enable NetworkManager
systemctl start NetworkManager
systemctl disable sshd.socket
systemctl enable sshd.service
chown -v root:messagebus /usr/libexec/dbus-daemon-launch-helper
chmod -v 4750 /usr/libexec/dbus-daemon-launch-helper
gtk-update-icon-cache -qtf /usr/share/icons/hicolor
update-desktop-database -q
gtk-query-immodules-2.0 --update-cache
gtk-query-immodules-3.0 --update-cache
gdk-pixbuf-query-loaders --update-cache
dconf update
echo 1 > /var/run/first-run
EOF

cat > ${SYSDIR}/sysroot/etc/first-run.d/999-create-user.sh << EOF
useradd -m loongson -c "default user"
echo loongson:loongson | chpasswd
usermod -a -G video loongson
usermod -a -G input loongson
usermod -a -G wheel loongson
echo "[User]
Email=
Session=
PasswordHint=
Icon=/var/lib/AccountsService/icons/loongson
SystemAccount=false" > /var/lib/AccountsService/users/loongson
cp /var/opt/default-user /var/lib/AccountsService/icons/loongson
EOF
